home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / Analyzer.c < prev    next >
Text File  |  1995-01-04  |  9KB  |  261 lines

  1. /* Analyzer.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "Analyzer.h"
  31. #include "MainWindowStuff.h"
  32. #include "AnalyzerSpec.h"
  33. #include "Memory.h"
  34. #include "InteractionWindow.h"
  35. #include "DataMunging.h"
  36. #include "Numbers.h"
  37.  
  38.  
  39. struct AnalyzerRec
  40.     {
  41.         MainWindowRec*            MainWindow;
  42.         AnalyzerSpecRec*        Spec;
  43.         MyBoolean                        StereoFlag;
  44.         float                                InverseVolume;
  45.  
  46.         largefixedsigned        LeftMinimum;
  47.         largefixedsigned        LeftMaximum;
  48.         largefixedsigned        RightMinimum; /* unused for mono */
  49.         largefixedsigned        RightMaximum; /* unused for mono */
  50.         long                                FrameCount;
  51.  
  52.         AnalyzerRec*                Next;
  53.     };
  54.  
  55.  
  56. static AnalyzerRec*                FreeList = NIL;
  57.  
  58.  
  59. /* flush free list elements */
  60. void                            FlushCachedAnalyzerStuff(void)
  61.     {
  62.         while (FreeList != NIL)
  63.             {
  64.                 AnalyzerRec*        Temp;
  65.  
  66.                 Temp = FreeList;
  67.                 FreeList = FreeList->Next;
  68.                 ReleasePtr((char*)Temp);
  69.             }
  70.     }
  71.  
  72.  
  73. /* create a new analyzer processor */
  74. AnalyzerRec*            NewAnalyzer(struct MainWindowRec* MainWindow,
  75.                                         struct AnalyzerSpecRec* Template, MyBoolean StereoFlag,
  76.                                         float InverseVolume)
  77.     {
  78.         AnalyzerRec*        Analyzer;
  79.  
  80.         CheckPtrExistence(MainWindow);
  81.         CheckPtrExistence(Template);
  82.         if (FreeList != NIL)
  83.             {
  84.                 Analyzer = FreeList;
  85.                 FreeList = FreeList->Next;
  86.             }
  87.          else
  88.             {
  89.                 Analyzer = (AnalyzerRec*)AllocPtrCanFail(sizeof(AnalyzerRec),"AnalyzerRec");
  90.                 if (Analyzer == NIL)
  91.                     {
  92.                         return NIL;
  93.                     }
  94.             }
  95.         Analyzer->MainWindow = MainWindow;
  96.         Analyzer->Spec = Template;
  97.         Analyzer->StereoFlag = StereoFlag;
  98.         Analyzer->InverseVolume = InverseVolume;
  99.         Analyzer->LeftMinimum = (signed long)0x7fffffff;
  100.         Analyzer->LeftMaximum = -(signed long)0x7fffffff;
  101.         Analyzer->RightMinimum = (signed long)0x7fffffff;
  102.         Analyzer->RightMaximum = -(signed long)0x7fffffff;
  103.         Analyzer->FrameCount = 0;
  104.         return Analyzer;
  105.     }
  106.  
  107.  
  108. /* dispose of the analyzer; write data to window */
  109. void                            DisposeAnalyzer(AnalyzerRec* Analyzer)
  110.     {
  111.         InteractionWindowRec*    Interaction;
  112.         char*                                    AnalyzerName;
  113.         char*                                    String;
  114.  
  115.         CheckPtrExistence(Analyzer);
  116.         Interaction = MainWindowGetInteractionWindow(Analyzer->MainWindow);
  117.         if (Interaction == NIL)
  118.             {
  119.                 return;
  120.             }
  121.         InteractionWindowAppendString(Interaction,
  122.             SYSTEMLINEFEED"Results for analyzer \x22",
  123.             StrLen(SYSTEMLINEFEED"Results for analyzer \x22"));
  124.         AnalyzerName = GetAnalyzerSpecString(Analyzer->Spec);
  125.         CheckPtrExistence(AnalyzerName);
  126.         InteractionWindowAppendString(Interaction,AnalyzerName,PtrSize(AnalyzerName));
  127.         InteractionWindowAppendString(Interaction,
  128.             "\x22:"SYSTEMLINEFEED"  Frames Processed:         ",
  129.             StrLen("\x22:"SYSTEMLINEFEED"  Frames Processed:         "));
  130.         String = IntegerToString(Analyzer->FrameCount);
  131.         if (String != NIL)
  132.             {
  133.                 InteractionWindowAppendString(Interaction,String,PtrSize(String));
  134.                 ReleasePtr((char*)String);
  135.             }
  136.         if (Analyzer->FrameCount != 0)
  137.             {
  138.                 if (Analyzer->StereoFlag)
  139.                     {
  140.                         InteractionWindowAppendString(Interaction,
  141.                             SYSTEMLINEFEED"  Left Channel Minimum:     ",
  142.                             StrLen(SYSTEMLINEFEED"  Left Channel Minimum:     "));
  143.                         String = LongDoubleToString(Analyzer->InverseVolume
  144.                             * largefixed2double(Analyzer->LeftMinimum),10,1e-4,1e6);
  145.                         if (String != NIL)
  146.                             {
  147.                                 InteractionWindowAppendString(Interaction,String,PtrSize(String));
  148.                                 ReleasePtr((char*)String);
  149.                             }
  150.                         InteractionWindowAppendString(Interaction,
  151.                             SYSTEMLINEFEED"  Left Channel Maximum:     ",
  152.                             StrLen(SYSTEMLINEFEED"  Left Channel Maximum:     "));
  153.                         String = LongDoubleToString(Analyzer->InverseVolume
  154.                             * largefixed2double(Analyzer->LeftMaximum),10,1e-4,1e6);
  155.                         if (String != NIL)
  156.                             {
  157.                                 InteractionWindowAppendString(Interaction,String,PtrSize(String));
  158.                                 ReleasePtr((char*)String);
  159.                             }
  160.                         InteractionWindowAppendString(Interaction,
  161.                             SYSTEMLINEFEED"  Right Channel Minimum:    ",
  162.                             StrLen(SYSTEMLINEFEED"  Right Channel Minimum:    "));
  163.                         String = LongDoubleToString(Analyzer->InverseVolume
  164.                             * largefixed2double(Analyzer->RightMinimum),10,1e-4,1e6);
  165.                         if (String != NIL)
  166.                             {
  167.                                 InteractionWindowAppendString(Interaction,String,PtrSize(String));
  168.                                 ReleasePtr((char*)String);
  169.                             }
  170.                         InteractionWindowAppendString(Interaction,
  171.                             SYSTEMLINEFEED"  Right Channel Maximum:    ",
  172.                             StrLen(SYSTEMLINEFEED"  Right Channel Maximum:    "));
  173.                         String = LongDoubleToString(Analyzer->InverseVolume
  174.                             * largefixed2double(Analyzer->RightMaximum),10,1e-4,1e6);
  175.                         if (String != NIL)
  176.                             {
  177.                                 InteractionWindowAppendString(Interaction,String,PtrSize(String));
  178.                                 ReleasePtr((char*)String);
  179.                             }
  180.                     }
  181.                  else
  182.                     {
  183.                         InteractionWindowAppendString(Interaction,
  184.                             SYSTEMLINEFEED"  Minimum:                  ",
  185.                             StrLen(SYSTEMLINEFEED"  Minimum:                  "));
  186.                         String = LongDoubleToString(Analyzer->InverseVolume
  187.                             * largefixed2double(Analyzer->LeftMinimum),10,1e-4,1e6);
  188.                         if (String != NIL)
  189.                             {
  190.                                 InteractionWindowAppendString(Interaction,String,PtrSize(String));
  191.                                 ReleasePtr((char*)String);
  192.                             }
  193.                         InteractionWindowAppendString(Interaction,
  194.                             SYSTEMLINEFEED"  Maximum:                  ",
  195.                             StrLen(SYSTEMLINEFEED"  Maximum:                  "));
  196.                         String = LongDoubleToString(Analyzer->InverseVolume
  197.                             * largefixed2double(Analyzer->LeftMaximum),10,1e-4,1e6);
  198.                         if (String != NIL)
  199.                             {
  200.                                 InteractionWindowAppendString(Interaction,String,PtrSize(String));
  201.                                 ReleasePtr((char*)String);
  202.                             }
  203.                     }
  204.             }
  205.         InteractionWindowAppendString(Interaction,
  206.             SYSTEMLINEFEED SYSTEMLINEFEED,StrLen(SYSTEMLINEFEED SYSTEMLINEFEED));
  207.         Analyzer->Next = FreeList;
  208.         FreeList = Analyzer;
  209.     }
  210.  
  211.  
  212. /* apply analyzer to some stuff */
  213. void                            ApplyAnalyzer(largefixedsigned* Data, long NumFrames,
  214.                                         AnalyzerRec* Analyzer)
  215.     {
  216.         long                        Scan;
  217.  
  218.         CheckPtrExistence(Analyzer);
  219.         CheckPtrExistence(Data);
  220.         Analyzer->FrameCount += NumFrames;
  221.         if (Analyzer->StereoFlag)
  222.             {
  223.                 for (Scan = 0; Scan < NumFrames; Scan += 1)
  224.                     {
  225.                         PRNGCHK(Data,&(Data[2 * Scan]),sizeof(Data[2 * Scan]));
  226.                         if (Data[2 * Scan] < Analyzer->LeftMinimum)
  227.                             {
  228.                                 Analyzer->LeftMinimum = Data[2 * Scan];
  229.                             }
  230.                         if (Data[2 * Scan] > Analyzer->LeftMaximum)
  231.                             {
  232.                                 Analyzer->LeftMaximum = Data[2 * Scan];
  233.                             }
  234.                         PRNGCHK(Data,&(Data[2 * Scan + 1]),sizeof(Data[2 * Scan + 1]));
  235.                         if (Data[2 * Scan + 1] < Analyzer->RightMinimum)
  236.                             {
  237.                                 Analyzer->RightMinimum = Data[2 * Scan + 1];
  238.                             }
  239.                         if (Data[2 * Scan + 1] > Analyzer->RightMaximum)
  240.                             {
  241.                                 Analyzer->RightMaximum = Data[2 * Scan + 1];
  242.                             }
  243.                     }
  244.             }
  245.          else
  246.             {
  247.                 for (Scan = 0; Scan < NumFrames; Scan += 1)
  248.                     {
  249.                         PRNGCHK(Data,&(Data[Scan]),sizeof(Data[Scan]));
  250.                         if (Data[Scan] < Analyzer->LeftMinimum)
  251.                             {
  252.                                 Analyzer->LeftMinimum = Data[Scan];
  253.                             }
  254.                         if (Data[Scan] > Analyzer->LeftMaximum)
  255.                             {
  256.                                 Analyzer->LeftMaximum = Data[Scan];
  257.                             }
  258.                     }
  259.             }
  260.     }
  261.